package com.codeest.geeknews.ui.main.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.codeest.geeknews.R;
import com.codeest.geeknews.model.bean.DoubanPlaylistBean.SongBean;
import com.codeest.geeknews.ui.main.fragment.DoubanMusicFragment;
import com.codeest.geeknews.widget.RotateableRoundImageView;

import java.util.List;


/**
 * Created by admin on 2017/8/18.
 */

public class DoubanMusicListAdapter extends RecyclerView.Adapter<DoubanMusicListAdapter.ViewHolder> {
    private List<SongBean> data;
    private DoubanMusicFragment mFragment;

    public interface OnMusicInfoListener {
        void onImageLoaded(RecyclerView.ViewHolder holder, int position, String url);
    }

    public DoubanMusicListAdapter(List<SongBean> data, DoubanMusicFragment musicFragment) {
        this.data = data;
        this.mFragment = musicFragment;
        if (!(mFragment instanceof OnMusicInfoListener)) {
            throw new RuntimeException(mFragment.getClass().getName() + "must implements " + OnMusicInfoListener.class.getName());
        }
    }

    @Override
    public DoubanMusicListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.item_music, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        ((RotateableRoundImageView) holder.image).stopRotation();
        final String url = data.get(position).getPicture();
        final String title = data.get(position).getTitle();
        final String artist = data.get(position).getArtist();
        holder.image.setTag(R.id.tag_title, title);
        holder.image.setTag(R.id.tag_artist, artist);
        Glide.with(mFragment)
                .load(url)
                .asBitmap()
                .skipMemoryCache(true)
                .into(new SimpleTarget<Bitmap>(300, 300) {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        holder.image.setImageDrawable(new BitmapDrawable(resource));
                        mFragment.onImageLoaded(holder, position, url);
                    }
                });
    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
        }
    }
}
